package javavis;

import java.awt.Image;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;

import javavis.base.JIPException;
import javavis.base.JIPToolkit;
import javavis.jip2d.base.JIPImage;
import javavis.jip2d.base.bitmaps.JIPImgBitmap;
import javavis.jip2d.functions.FColorToGray;
import javavis.jip3d.geom.Point3D;

/**
*   This executable class allows to clean 3D points from a SR4000 camera with low confidence
*/
public class CleanSR4000 {
	/**
	 * Directory where to apply the cleaning
	 * @uml.property  name="dir"
	 */
	private String dir;
	
	public static final int THRESHOLD=100;

	
	/** Constructor. */
	public CleanSR4000(String idir) {
		dir=idir;
	}

	/**
	 * Shows an error and help lines in standard error output.
	 * @param str Error to show.
	 */
	public void error(String str) {
		System.err.println("*** ERROR: " + str + " ***");
		help();
	}

	/**
	* It shows in the standard error output the function help lines.
	*/
	static public void help() {
		System.out.println("Usage:");
		System.out.println("    java CleanSR4000 dir");
		System.out.println("  where dir is the directory to process. It must contain the files to process and");
		System.out.println("  the confidence image.");
	}
	
	/**
	 * Execute the cleaning
	 */
	public void execute () {
		File f = new File(dir);
		String []files = f.list();
		Reader fr;
		StreamTokenizer st;
		int num_puntos;
		boolean imageValues, yInverted;
		int isColor;
		FileWriter fw;

		for (String fi : files)
			if (fi.matches("\\w*.p3d")) {
				System.out.println("Processing "+fi);

				try {
					String aux1=new String();
					String aux2=new String();
					fr=new FileReader(dir+"//"+fi);
					st=new StreamTokenizer(fr);

					st.nextToken();
					char c=(char)st.ttype;
					aux1 += c;
					st.nextToken();
					aux1 += st.sval;
					aux1 += "\n";
					st.nextToken();
					num_puntos=(int)st.nval;
					st.nextToken();
					isColor=(int)st.nval;
					aux2 += " "+(int)st.nval;
					st.nextToken();
					imageValues=((int)st.nval==1);
					aux2 += " "+(int)st.nval;
					st.nextToken();
					yInverted=((int)st.nval==1);
					aux2 += " "+(int)st.nval+"\n";
					st.nextToken();
					c=(char)st.ttype;
					aux2 += c;
					st.nextToken();
					aux2 += st.sval+"\n";
					
					String pre = fi.substring(0, 3);
					String post = fi.substring(3, 7);
					Image img = JIPToolkit.getAWTImage(dir+"//"+pre+"_c_"+post+".png");
					if (img == null)
						System.out.println("Error: "+pre+"_c_"+post+" not found");;
					JIPImage jipimg = JIPToolkit.getColorImage(img);
					FColorToGray fcg = new FColorToGray();
					JIPImgBitmap jip = (JIPImgBitmap)fcg.processImg(jipimg);
					
					int np=0;
					Point3D[] ps = new Point3D[num_puntos];
					for(int cont=0;cont<num_puntos;cont++) {
						Point3D paux= new Point3D(st, isColor, imageValues, yInverted);
						if (jip.getPixel(paux.posx, paux.posy)>THRESHOLD) {
							ps[np] = paux;
							np++;
						}
					}
					fr.close();

					fw = new FileWriter(dir+"b//"+fi);
					fw.write(aux1+np+aux2);
					for(int cont=0;cont<np;cont++) {
						fw.write(ps[cont].toStringRGB()+"\n");
					}
					fw.close();

				} catch (IOException e)
				{
					System.out.println("CleanSR4000::readData Error: can not read data from "+dir+"//"+fi);
					return;
				}catch (JIPException e)
				{
					System.out.println("CleanSR4000::readData Error:"+e);
					return;
				}

			}
	}

	public static void main(String[] args) {
		if (args==null || args.length==0) {
			CleanSR4000.help();
			System.exit(-1);
		}
		CleanSR4000 lau = new CleanSR4000(args[0]);
		lau.execute();
		System.exit(0);
	}
}
